﻿#include "mysqlDB.h"


#include <QSettings>
#include <QFileInfo>

MySQLDB::MySQLDB()
{

}

MySQLDB::~MySQLDB()
{

}

void MySQLDB::initialize(char *host, char *user,  char *pwd, char *dbname, int port)
{
    m_host  = host;
    m_user  = user;
    m_pwd   = pwd;
    m_db    = dbname;
    m_port  = port;
}

//连接数据库
bool MySQLDB::ConnectDatabase()
{
    //初始化mysql
    mysql_init(&mysql);

    //返回false则连接失败，返回true则连接成功
    if (!(mysql_real_connect(&mysql, m_host.c_str(), m_user.c_str(), m_pwd.c_str(), m_db.c_str(), m_port, NULL, 0)))
    {
        LOG(ERROR) << "Connect to Database Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        m_isConnect = false;
        return false;
    }
    else
    {
        m_isConnect = true;
        return true;
    }
}
//释放资源
void MySQLDB::FreeConnect()
{
    mysql_close(&mysql);	 //关闭一个服务器连接。
    m_isConnect = false;
}

/***************************数据库操作***********************************/
//插入数据
bool MySQLDB::InsertData(char *sql)
{
    if (mysql_query(&mysql, sql))        //执行SQL语句
    {
        LOG(ERROR) << "InsertData Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        return false;
    }
    return true;
}

//修改数据
bool MySQLDB::ModifyData(char *sql)
{
    if (mysql_query(&mysql, sql))        //执行SQL语句
    {
        LOG(ERROR) << "ModifyData Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        return false;
    }
    return true;
}

//删除数据
bool MySQLDB::DeleteData(char *sql)
{
    if (mysql_query(&mysql, sql))        //执行SQL语句
    {
        LOG(ERROR) << "DeleteData Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        return false;
    }
    return true;
}

bool MySQLDB::SelectData(const char *sql, std::unordered_set<std::string> &result, unsigned int &rows)
{

    char utf8[50];
    memset(utf8, 0, sizeof(utf8));
    strcpy_s(utf8, "set names utf8");
    if(mysql_query(&mysql, utf8))
    {
        LOG(ERROR) << "set utf8 Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        return false;
    }


    rows = 0;

    if(mysql_query(&mysql, sql))
    {
        LOG(ERROR) << "SelectData Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        return false;
    }
    res = mysql_store_result(&mysql);			// 未查到记录

    if (res)
    {
        int colCount = mysql_num_fields(res);
        int rowCount = mysql_num_rows(res);
        rows = rowCount;

        fd = mysql_fetch_fields(res);

        while((row = mysql_fetch_row(res)))
        {
            unsigned long *lengths;
            lengths = mysql_fetch_lengths(res);

            for(int i = 0; i < colCount; i++)
            {
                result.emplace(row[i]? row[i] : "NULL");
            }
        }
    }
    else
    {
        if(mysql_errno(&mysql))
        {
            LOG(ERROR) << "mysql_store_result Failed!!!  Err:" << mysql_error(&mysql) << std::endl;

        }
        else if(mysql_field_count(&mysql) == 0)
        {
            LOG(ERROR) << "query does not return data!  Err:" << mysql_error(&mysql) << std::endl;
        }

        mysql_free_result(res);
        return false;
    }

    mysql_free_result(res);
    return true;
}
// select data
bool MySQLDB::SelectData(const char *sql, std::unordered_map<std::string, std::string> &result, unsigned int &colCount, unsigned int &rowCount)
{
    colCount = 0;
    rowCount = 0;

    if(mysql_query(&mysql, sql))
    {
        LOG(ERROR) << "SelectData Failed!!!  Err:" << mysql_error(&mysql) << std::endl;
        return false;
    }

    res = mysql_store_result(&mysql);			// 未查到记录

    if (res)
    {
        colCount = mysql_num_fields(res);
        rowCount = mysql_num_rows(res);

        fd = mysql_fetch_fields(res);


        while((row = mysql_fetch_row(res)))
        {
            unsigned long *lengths;
            lengths = mysql_fetch_lengths(res);

            for(int i = 0; i < (int)colCount; i++)
            {
                result.emplace(std::make_pair(fd[i].name, (row[i]? row[i] : "NULL")));
            }
        }
    }
    else
    {
        if(mysql_errno(&mysql))
        {
            LOG(ERROR) << "mysql_store_result Failed!!!  Err:" << mysql_error(&mysql) << std::endl;

        }
        else if(mysql_field_count(&mysql) == 0)
        {
            LOG(ERROR) << "query does not return data!  Err:" << mysql_error(&mysql) << std::endl;
        }

        mysql_free_result(res);
        return false;
    }

    mysql_free_result(res);
    return true;
}

bool MySQLDB::IsConnect()
{
    return m_isConnect;
}


